package com.pocketsweet.chatlib.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.FollowCallback;
import com.avos.avoscloud.FunctionCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;
import com.easemob.EMCallBack;
import com.easemob.chat.CmdMessageBody;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.EMNotifier;
import com.easemob.chat.TextMessageBody;
import com.pocketsweet.C;
import com.pocketsweet.MLApplication;
import com.pocketsweet.MLCache;
import com.pocketsweet.chat.ui.activity.ChatActivity;
import com.pocketsweet.chatui.Constant;
import com.pocketsweet.dao.SystemConfigManager;
import com.pocketsweet.model.MLDate;
import com.pocketsweet.model.MLFriendApply;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.Main;
import com.pocketsweet.ui.Profile;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

public class MolianContactManager {
    private static MolianContactManager _instance = null;
    private MolianContactListerner mMolianContactListerner;
    private MoLianAgreeFridedListener mMoLianAgreeFridedListener;
    public Context context;

    public MolianContactManager() {
        context = MLApplication.getContext();

    }

    public static MolianContactManager getInstance() {
        if (_instance == null) {
            _instance = new MolianContactManager();
        }
        return _instance;
    }

    /*
     * 申请加为好友
     */
    public void addContact(final MLUser user, final String reason) {
        // 保存一条申请记录，并发一条文本消息
        final MLUser cunrrentUser = UserService.getCurrentUser();
        AVQuery<MLFriendApply> query = new AVQuery<MLFriendApply>(
                "MLFriendApply");
        query.whereEqualTo("user", user);
        query.whereEqualTo("applier", cunrrentUser);
        query.findInBackground(new FindCallback<MLFriendApply>() {

            @Override
            public void done(List<MLFriendApply> applys, AVException e) {
                if (e == null) {
                    if (applys.size() == 0) {
                        MLFriendApply mlFriendApply = new MLFriendApply();
                        mlFriendApply.setApplier(cunrrentUser);
                        mlFriendApply.setUser(user);
                        mlFriendApply.setRemark(reason);
                        mlFriendApply.saveInBackground(new SaveCallback() {

                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    // 发送
                                    Map<String, Object> params = new HashMap<String, Object>();
                                    params.put("touserId", user.getObjectId());
                                    params.put("fromuserId",
                                            cunrrentUser.getObjectId());
                                    params.put("nick",
                                            cunrrentUser.getNickname());
                                    params.put("actions", C.ACTION_FRIEND_APPLY);
                                    AVCloud.callFunctionInBackground(
                                            "sendFriendApplyMsgShow", params,
                                            new FunctionCallback<Object>() {

                                                @Override
                                                public void done(Object arg0,
                                                                 AVException e) {
                                                    if (e == null) {
                                                        ToolKits.toast(context,
                                                                "发送邀请成功");
                                                    } else {
                                                        ToolKits.toast(context,
                                                                "发送邀请失败，请检查网络");
                                                    }
                                                }

                                            });

                                } else {
                                    ToolKits.toast(context, "发送邀请失败，请检查网络");
                                }
                            }
                        });
                    } else {
                        ToolKits.toast(context, "您已经申请过了，等待Ta的回应哦");
                    }
                } else {
                    ToolKits.toast(context, "发送邀请失败");
                }

            }
        });

    }

    /*
     * 同意加为好友
     */
    public void agreeAddContact(final MLUser user, final String applyId,
                                final View v) {
        final String toUserId = user.getObjectId();
        // 先发送一条透传消息告知对方已经通过申请，然后再发一条灰条消息通知对方
        final MLUser cunrrentUser = UserService.getCurrentUser();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("touserId", toUserId);
        params.put("fromuserId", cunrrentUser.getObjectId());
        params.put("nick", cunrrentUser.getNickname());
        params.put("actions", C.ACTION_FRIEND_APPLY_ACCEPTED);
        AVCloud.callFunctionInBackground("sendFriendApplyMsg", params,
                new FunctionCallback<Object>() {

                    @Override
                    public void done(Object arg0, AVException e) {
                        if (e == null) {
                            // 添加好友
                            // MLUser mlUser = new MLUser();
                            // mlUser.setObjectId(toUserId);
                            // mlUser.followInBackground(cunrrentUser.getObjectId(),
                            // null);
                            cunrrentUser.followInBackground(toUserId,
                                    new FollowCallback<AVObject>() {

                                        @Override
                                        public void done(AVObject arg0,
                                                         AVException e) {
                                            if (e == null) {
                                                // 保存好友
                                                MLCache.saveFriend(user);
                                                // 发送灰条的通知消息(这里先用文本代替)
                                                EMConversation conversation = EMChatManager
                                                        .getInstance()
                                                        .getConversation(
                                                                cunrrentUser
                                                                        .getObjectId());
                                                EMMessage message = EMMessage
                                                        .createSendMessage(EMMessage.Type.TXT);
                                                String content = "我同意了让你做我的口袋恋人,要好好对我哦";
                                                TextMessageBody txtBody = new TextMessageBody(
                                                        content);
                                                message.addBody(txtBody);

                                                // 增加自己特定的属性,消息类型
                                                // message.setAttribute(
                                                // "type",
                                                // C.EMMESSAGE_EXT_TYPE_CHAT_NOTIFICATION_MSG);
                                                message.setReceipt(user
                                                        .getObjectId());
                                                conversation
                                                        .addMessage(message);
                                                // 发送消息
                                                EMChatManager
                                                        .getInstance()
                                                        .sendMessage(
                                                                message,
                                                                new EMCallBack() {

                                                                    @Override
                                                                    public void onError(
                                                                            int arg0,
                                                                            String arg1) {
                                                                        mMoLianAgreeFridedListener
                                                                                .onAgressFail();
                                                                    }

                                                                    @Override
                                                                    public void onProgress(
                                                                            int arg0,
                                                                            String arg1) {
                                                                        // TODO
                                                                        // Auto-generated
                                                                        // method
                                                                        // stub

                                                                    }

                                                                    @Override
                                                                    public void onSuccess() {
                                                                        mMoLianAgreeFridedListener
                                                                                .onAgreeeSucess(
                                                                                        applyId,
                                                                                        v);
                                                                    }
                                                                });
                                            } else {
                                                mMoLianAgreeFridedListener
                                                        .onAgressFail();
                                            }

                                        }
                                    });

                        } else {
                            mMoLianAgreeFridedListener.onAgressFail();
                        }
                    }

                });

    }

    /*
     * 清除缓存
     */
    public void deleteCache() {
        // 删除所有会话
        // EMChatManager.getInstance().deleteAllConversation();
        // 删除所有图片
        UserService.imageLoader.clearMemoryCache();
        UserService.imageLoader.clearDiskCache();
    }

    /*
     * 分手解除关系
     */
    public void deleteFriend(final MLUser user, final String reason) {
        final MLUser cunrrentUser = UserService.getCurrentUser();
        final String toUserId = user.getObjectId();
        final String reasonString = reason.equals("") ? cunrrentUser
                .getNickname() + "跟你解除了恋人关系" : cunrrentUser.getNickname()
                + "跟你解除了恋人关系，原因：" + reason;

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("touserId", toUserId);
        params.put("fromuserId", cunrrentUser.getObjectId());
        params.put("nick", cunrrentUser.getNickname());
        params.put("actions", C.ACTION_FRIEND_DELETE);
        AVCloud.callFunctionInBackground("sendFriendApplyMsg", params,
                new FunctionCallback<Object>() {

                    @Override
                    public void done(Object arg0, AVException e) {
                        if (e == null) {
                            // 取消follo并删除本地好友
                            cunrrentUser.unfollowInBackground(toUserId,
                                    new FollowCallback<AVObject>() {

                                        @Override
                                        public void done(AVObject arg0,
                                                         AVException e) {
                                            if (e == null) {
                                                // 删除本地好友
                                                MLCache.deletFriend(toUserId);
                                                // 给对方发一条分手的消息
                                                Map<String, Object> params = new HashMap<String, Object>();
                                                params.put("touserId", toUserId);
                                                params.put("msg", reasonString);
                                                AVCloud.callFunctionInBackground(
                                                        "sendSysMsg", params,
                                                        null);
                                                unfollow(toUserId);

                                                // 删除与此人的会话
                                                EMChatManager.getInstance()
                                                        .deleteConversation(
                                                                toUserId);
                                                // 如果是在聊天页面删除的好友,怎关闭页面
                                                if (ChatActivity.activityInstance != null) {
                                                    if (toUserId
                                                            .equals(ChatActivity.activityInstance
                                                                    .getToChatUsername())) {
                                                        ChatActivity.activityInstance
                                                                .finish();
                                                    }
                                                }

                                            } else {
                                                ToolKits.toast(context,
                                                        "分手失败，请减查网络");
                                            }
                                        }

                                    });

                        } else {
                            ToolKits.toast(context, "分手失败，请减查网络");
                        }
                    }
                });

    }

    // 删掉对方的follow列表
    private void unfollow(String userId) {
        AVQuery<MLUser> queryUser = new AVQuery<MLUser>("_User");
        queryUser.getInBackground(userId, new GetCallback<MLUser>() {

            @Override
            public void done(final MLUser user, AVException e) {
                // TODO Auto-generated method stub
                if (e == null) {
                    if (user != null) {
                        AVQuery<MLUser> query = new AVQuery<MLUser>("_Follower");
                        query.whereEqualTo("user", UserService.getCurrentUser());
                        query.whereEqualTo("follower", user);
                        query.deleteAllInBackground(new DeleteCallback() {

                            @Override
                            public void done(AVException arg0) {
                                // TODO Auto-generated method stub
                                if (arg0 == null) {
                                    AVQuery<MLUser> query = new AVQuery<MLUser>(
                                            "_Followee");
                                    query.whereEqualTo("user", user);
                                    query.whereEqualTo("followee",
                                            UserService.getCurrentUser());
                                    query.deleteAllInBackground(new DeleteCallback() {

                                        @Override
                                        public void done(AVException arg0) {
                                            // TODO Auto-generated method stub

                                        }
                                    });
                                } else {
                                }
                            }
                        });
                    }
                } else {

                }

            }
        });

    }

    /*
     * 解除通话权限
     */
    public void deleteStanger(final MLUser user) {
        final MLUser cunrrentUser = UserService.getCurrentUser();
        final String toChatUsername = user.getObjectId();
        AVQuery<MLDate> datequery1 = new AVQuery<MLDate>("MLDate");
        datequery1.whereEqualTo("user", user);
        datequery1.whereEqualTo("sponsor", cunrrentUser);

        AVQuery<MLDate> datequery2 = new AVQuery<MLDate>("MLDate");
        datequery2.whereEqualTo("user", cunrrentUser);
        datequery2.whereEqualTo("sponsor", user);

        List<AVQuery<MLDate>> queries = new ArrayList<AVQuery<MLDate>>();
        queries.add(datequery1);
        queries.add(datequery2);

        final AVQuery<MLDate> mainQuery = AVQuery.or(queries);
        mainQuery.include("user");
        mainQuery.findInBackground(new FindCallback<MLDate>() {

            @Override
            public void done(final List<MLDate> dates, AVException e) {
                if (e == null) {
                    if (dates.size() != 0) {
                        mainQuery.deleteAllInBackground(null);

                        // 发送解除通话透传
                        EMMessage cmdMsg = EMMessage
                                .createSendMessage(EMMessage.Type.CMD);
                        String action = C.ACTION_DISABLE_CHAT;// action可以自定义，在广播接收时可以收到
                        CmdMessageBody cmdBody = new CmdMessageBody(action);
                        cmdMsg.setReceipt(toChatUsername);
                        cmdMsg.setAttribute("from", cunrrentUser.getObjectId());
                        cmdMsg.addBody(cmdBody);
                        EMChatManager.getInstance().sendMessage(cmdMsg,
                                new EMCallBack() {

                                    @Override
                                    public void onSuccess() {
                                        // 删除与此人的会话
                                        EMChatManager.getInstance()
                                                .deleteConversation(
                                                        toChatUsername);
                                        // 给对方发一条解除通话权限的消息
                                        Map<String, Object> params = new HashMap<String, Object>();
                                        params.put("touserId", toChatUsername);
                                        params.put("msg",
                                                cunrrentUser.getNickname()
                                                        + "和你解除了通话权限");
                                        AVCloud.callFunctionInBackground(
                                                "sendSysMsg", params, null);
                                        // 如果是在聊天页面删除的好友,怎关闭页面
                                        if (ChatActivity.activityInstance != null) {
                                            if (toChatUsername
                                                    .equals(ChatActivity.activityInstance
                                                            .getToChatUsername())) {
                                                ChatActivity.activityInstance
                                                        .finish();
                                            }
                                        }
                                        // 如果个人资料页面存在的话，则改为不能直接进入聊天页面
                                        if (Profile.userProfileInstance != null) {
                                            if (toChatUsername
                                                    .equals(Profile.userProfileInstance
                                                            .getCurUserId())) {
                                                Profile.userProfileInstance
                                                        .finish();
                                            }
                                        }
                                    }

                                    @Override
                                    public void onProgress(int arg0, String arg1) {

                                    }

                                    @Override
                                    public void onError(int arg0, String arg1) {

                                    }
                                });
                    }
                } else {
                    // Log.e("==error","error:"+e.getMessage());
                    // ToolKits.toast(context, "请检查网络");
                }
            }
        });
    }

    public void onUpdate(final String userId) {

        AVQuery<MLUser> query = new AVQuery<MLUser>("_User");
        query.getInBackground(userId, new GetCallback<MLUser>() {

            @Override
            public void done(MLUser mluser, AVException e) {
                if (e == null) {
                    MLCache.getCache().put(mluser.getObjectId(),
                            mluser.toJSONObject());
                }
            }
        });

    }

    public String getContactChangeAction() {
        return null;
    }

    public void setContactListener(MolianContactListerner l) {
        if (null != l && mMolianContactListerner != l) {
            mMolianContactListerner = l;
        }
    }

    public interface MolianContactListerner {
        public void onContactAdded(List<String> usernameList);

        public void onContactDeleted(String userId);

        public void onContactInvited(EMMessage message);

        public void onContactAgreed(String userId);

        public void onContactRefused(String username);

        public void onChatApplyAgree(String userId);

        public void onNewLike(EMMessage message);

        public void onNewRecommend();

        public void onChatConversationDelete(String userId);

        public void onForbiddenUser(String userId);

        public void onDisableUser(String userId);

        public void onCallLiked(String userId);

        public void onCustomazitionCall(String userId);

        public void onNewCustomazition();

        // 新单的处理方式
        public void onTestOrderBeReplyed(EMMessage message);// 试聊单被回复

        public void onNewOrderBeCreated(EMMessage message);// 恋爱体验师收到新的订单的通知

        public void onNewOrderBeReplyed(EMMessage message);// 新的订单被回复；

        public void onTimeAddedByLover(EMMessage message);// 订单被恋爱体验师爱心加时；

        public void onTestOrderFinish(EMMessage message);

        public void onOrderFinish(EMMessage message);

    }

    public void onTestOrderFinish(EMMessage message) {
        mMolianContactListerner.onTestOrderFinish(message);
    }

    public void onOrderFinish(EMMessage message) {
        mMolianContactListerner.onOrderFinish(message);
    }

    public void onTimeAddedByLover(EMMessage message) {
        mMolianContactListerner.onTimeAddedByLover(message);
    }

    public void onTestOrderBeReplyed(EMMessage message) {
        mMolianContactListerner.onTestOrderBeReplyed(message);
    }

    public void onNewOrderBeCreated(EMMessage message) {
        mMolianContactListerner.onNewOrderBeCreated(message);
    }

    public void onNewOrderBeReplyed(EMMessage message) {
        mMolianContactListerner.onNewOrderBeReplyed(message);
    }

    public void setMoLianAgreeFridedListener(MoLianAgreeFridedListener l) {
        if (null != l && mMoLianAgreeFridedListener != l) {
            mMoLianAgreeFridedListener = l;
        }
    }

    public interface MoLianAgreeFridedListener {
        public void onAgreeeSucess(String applyId, View v);

        public void onAgressFail();
    }

    public void onContactInvited(EMMessage message) {
        mMolianContactListerner.onContactInvited(message);
    }

    public void onContactAgreed(String userId) {
        mMolianContactListerner.onContactAgreed(userId);
    }

    public void onChatApplyAgree(String userId) {
        mMolianContactListerner.onChatApplyAgree(userId);
    }

    public void onContactDeleted(String userId) {
        mMolianContactListerner.onContactDeleted(userId);
    }

    public void onNewLike(EMMessage message) {
        mMolianContactListerner.onNewLike(message);
    }

    public void onNewRecommend() {
        mMolianContactListerner.onNewRecommend();
    }

    public void onCustomazitionCall(String userId) {
        mMolianContactListerner.onCustomazitionCall(userId);
    }

    public void onNewCustomazition() {
        mMolianContactListerner.onNewCustomazition();
    }

    // 随机通话的时候有人喜欢了自己
    public void onCallLiked(String userId) {
        mMolianContactListerner.onCallLiked(userId);
    }

    public void onChatConversationDelete(String userId) {
        mMolianContactListerner.onChatConversationDelete(userId);
    }

    public void onForbiddenUser(String userId) {
        mMolianContactListerner.onForbiddenUser(userId);
    }

    public void onDisableUser(String userId) {
        mMolianContactListerner.onDisableUser(userId);
    }

    // 推送消息
    public void onNewSysMsg(String msg) {
        // Log.e("xiaoxi",msg);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("touserId", UserService.getCurrentUserId());
        params.put("msg", msg);
        AVCloud.callFunctionInBackground("sendSysMsg", params, null);

    }

    // 推送的新的文章
    public void onNewRecommedPush(String msg) {
        // Log.e("msg",msg);
        EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
        CmdMessageBody cmdBody = new CmdMessageBody(C.ACTION_RECOMMENT);
        cmdMsg.setReceipt(UserService.getCurrentUserId());
        cmdMsg.setFrom(Constant.SYSTEM);
        cmdMsg.setAttribute("msg", msg);
        cmdMsg.addBody(cmdBody);
        EMChatManager.getInstance().sendMessage(cmdMsg, null);
    }

    // 推送的新的恋爱呼叫
    public void onNewCustomizationPush(String msg) {
        // Log.e("msg",msg);
        EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
        CmdMessageBody cmdBody = new CmdMessageBody(C.ACTION_NEW_CUSTOMIZATION);
        cmdMsg.setReceipt(UserService.getCurrentUserId());
        cmdMsg.setFrom(Constant.SYSTEM);
        cmdMsg.setAttribute("msg", msg);
        cmdMsg.addBody(cmdBody);
        EMChatManager.getInstance().sendMessage(cmdMsg, null);
    }

    // 在本地保存灰条消息
    public void saveNewGrayMsg(String touserId, String touserName, int type,
                               String time) {
        String content = "";
        if (StringUtils.isEmpty(touserName)) {
            touserName = "Ta";
        }
        switch (type) {
            case C.GRAY_MEG_NORMAL_FIRST:
                content = "正在召唤" + touserName + "，请等待一会哦";
                break;
            case C.GRAY_MEG_NORMAL_REPPLIED:
                content = touserName + "将做为你的临时恋人，陪伴你度过20分钟的时光";
                break;
            case C.GRAY_MEG_NORMAL_REST_FIVE:
                content = "和Ta相处时间剩最后5分钟了，要抓紧哦。";
                break;
            case C.GRAY_MEG_NORMAL_TIMEOUT_TEST:
                content = "短暂的陪伴结束了，喜欢的话就领走吧";
                break;
            case C.GRAY_MEG_NORMAL_TIMEOUT:
                content = "短暂的陪伴结束了，对" + touserName
                        + "的印象如何呐？到个人页面，给Ta打个分吧。喜欢可以继续领走哦";

                break;
            case C.GRAY_MEG_NORMAL_ADDED:
                content = touserName + "给了你爱心加时，聊天时间多了10分钟，看来" + touserName
                        + "很喜欢你哦～";
                // sendSysMsg(touserId, content);
                break;
            case C.GRAY_MEG_NORMAL_TIMEADD:
                content = "正在召唤" + touserName + "，请等待一会哦。如果半小时内恋人没有回应，时光稍后会返还到你的账户";
                break;
            case C.GRAY_MEG_LOVER_REPPLY:
                content = "你将作为" + touserName + "的临时恋人，争取让Ta领走哦";
                break;
            case C.GRAY_MEG_LOVER_REST_FIVE:
                content = "相处时间还剩5分钟哦，点击右上角可以领走恋人";
                break;
            case C.GRAY_MEG_LOVER_TIMEADDED:
                content = touserName + "已经付款领走你" + time + "，快去和恋人聊聊吧。";
                // sendSysMsg(UserService.getCurrentUserId(), content);
                break;
            case C.GRAY_MEG_LOVER_ADD:
                content = "你给了" + touserName + "爱心加时，聊天时间多了10分钟，抓紧时间留住Ta吧";
                break;
            case C.GRAY_MEG_LOVER_TIMEOUT:
                content = "短暂的陪伴结束了，等等看对方给你的评价哦";
                break;
            case C.GRAY_MEG_TEST_LOVER_TIMEOUT:
                content = "短暂的陪伴结束了，看来没有被领走哦，多多努力加油吧";

                break;
        }

        saveGrayMsg(touserId, content);

    }

    public void saveGrayMsg(String touserId, String content) {
        EMMessage message = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
        message.setFrom(touserId);
        message.setAttribute("type", C.EMMESSAGE_EXT_TYPE_CHAT_NOTIFICATION_MSG);

        message.addBody(new TextMessageBody(content));
        message.setMsgId(UUID.randomUUID().toString());
        message.setMsgTime(System.currentTimeMillis());
        EMChatManager.getInstance().saveMessage(message, true);
        EMChatManager.getInstance().importMessage(message, true);
    }

    public void sendCMDMsg(String fromUserNickname, String touserId,
                           String action, EMCallBack callback) {
        EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
        CmdMessageBody cmdBody = new CmdMessageBody(action);
        cmdMsg.setReceipt(touserId);
        cmdMsg.setAttribute("nickname", fromUserNickname);
        cmdMsg.addBody(cmdBody);
        EMChatManager.getInstance().sendMessage(cmdMsg, callback);
    }

    public void sendSysMsg(String touserId, String content) {
        // EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
        // message.setReceipt(touserId);
        // message.setFrom(Constant.SYSTEM);
        // message.addBody(new TextMessageBody(content));
        // EMChatManager.getInstance().sendMessage(message, null);
        // Log.e("==", "send msg to myself");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("touserId", touserId);
        params.put("msg", content);
        AVCloud.callFunctionInBackground("sendSysMsg", params,
                new FunctionCallback<Object>() {

                    @Override
                    public void done(Object arg0, AVException e) {
                        if (e == null) {
                        }

                    }
                });
    }

    public void sendCMDMsg_ADDTime(String fromUserNickname, String touserId,
                                   String action, String time, EMCallBack callback) {
        EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
        CmdMessageBody cmdBody = new CmdMessageBody(action);
        cmdMsg.setReceipt(touserId);
        cmdMsg.setAttribute("nickname", fromUserNickname);
        cmdMsg.setAttribute("time", time);
        cmdMsg.addBody(cmdBody);
        EMChatManager.getInstance().sendMessage(cmdMsg, callback);
    }

    public void sendCMDMsg_ADDOrderId(String orderId, String fromUserNickname,
                                      String touserId, String action, EMCallBack callback) {
        EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
        CmdMessageBody cmdBody = new CmdMessageBody(action);
        cmdMsg.setReceipt(touserId);
        cmdMsg.setAttribute("nickname", fromUserNickname);
        cmdMsg.setAttribute("orderId", orderId);
        cmdMsg.addBody(cmdBody);
        EMChatManager.getInstance().sendMessage(cmdMsg, callback);
    }

}
